package com.qd.p2p.web.action.user;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.UserPwdQuestionService;
import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.service.user.UserEmailService;
import com.qdlc.p2p.biz.util.ValidCodeSender;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.NoticeTplNid;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserPwdQuestion;

public class UserEmailAction extends BaseAction<User> implements ModelDriven<User>{

	@Resource
	UserPwdQuestionService userPwdQuestionService;
	
	@Resource
	UserService userService;
	
	@Resource
	UserEmailService userEmailService;
	
    /**
     * [修改邮箱]跳转到修改邮箱页面
     */
    @Action("/member/security/modifyEmail")
    public String modifyEmail() throws Exception {
        User user = getSessionUser();
        List<UserPwdQuestion> pwdQuestionList = userPwdQuestionService.listRand(user.getUserId());
        if (pwdQuestionList != null && pwdQuestionList.size() > 0) {
            request.setAttribute("pwdQuestionList", pwdQuestionList);
        } else {
            this.frontRedirect("/member/security/pwdQuestion.html");
        }
        return "modifyEmail";
    }
	
    /**
     * [修改邮箱]获取验证码
     * @throws Exception 
     */
    @Action("/member/security/modifyEmailCode")
    public void modifyEmailCode() throws Exception {
        User user = getSessionUser();
        ReturnResult result = ValidCodeSender.newInstance().sendCodeByEmail(user, user.getEmail(),
                NoticeTplNid.NOTICE_MODIFY_EMAIL);
        printWebResult(result.getErrorInfo(), result.isSuccess());
    }

    /**
     * [修改邮箱]校验验证码
     * @throws Exception 
     */
    @Action("/member/security/checkModifyEmailCode")
    public void checkModifyEmailCode() throws Exception {
        User user = getSessionUser();
        String code = paramString("code");
        ReturnResult result = ValidCodeSender.checkValidCode(user, code, NoticeTplNid.NOTICE_MODIFY_EMAIL);
        printWebResult(result.getErrorInfo(), result.isSuccess());
    }

    /**
     * [绑定邮箱]获取验证码
     * @throws Exception
     */
    @Action("/member/security/bindEmailCode")
    public void bindEmailCode() throws Exception {
        User user = getSessionUser();
        ReturnResult result = ValidCodeSender.newInstance().sendCodeByEmail(user, model.getEmail(),
                NoticeTplNid.NOTICE_BIND_EMAIL);
        printWebResult(result.getErrorInfo(), result.isSuccess());
    }

    /**
     * [绑定邮箱] 绑定邮箱
     */
    @Action("/member/security/doBindEmail")
    public void doBindEmail() throws Exception {
    	//获取请求参数
        User user = getSessionUser();
        String code = paramString("code");
        String email = paramString("email");

        //修改邮箱,如果成功则修改User和UserIdentity信息
        ReturnResult result = userEmailService.modifyEmail(user, code, email);
        printWebResult(result.getErrorInfo(), result.isSuccess());
    }
}
